package com.ruoyi.member.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.vo.MemberInfoVo;
import com.ruoyi.common.enums.UserBusinessSubType;
import com.ruoyi.common.enums.UserBusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.member.domain.MemberInfo;
import com.ruoyi.member.mapper.MemberInfoMapper;
import com.ruoyi.member.service.IMemberInfoService;
import com.ruoyi.member.service.impl.MemberInfoServiceImpl;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeptService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Description: 工作人员通用接口
 * Author: YangShiWei
 * Email: strongwalter2014@gmail.com
 * Datetime: 2023/2/1 22:10
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/member/common")
public class MemberCommonController {

    private final ISysDeptService sysDeptService;
    private final SysUserMapper sysUserMapper;
    private final MemberInfoMapper memberInfoMapper;
    private final MemberInfoServiceImpl memberInfoService;


    /**
     * 查询当前用户所在机构下所有下级部门的工作人员信息
     *
     * @param type
     * @return
     */
    @GetMapping("listMemberByType")
    public R<List<MemberInfoVo>> listMemberByType(@RequestParam(required = false) UserBusinessSubType type) {
//        List<Long> deptIds = sysDeptService.selectNormalChildrenDeptIdById(LoginHelper.getDeptId());
//        deptIds.add(LoginHelper.getDeptId());
//        LambdaQueryWrapper<SysUser> wrapperForUser = Wrappers.<SysUser>lambdaQuery().in(SysUser::getDeptId, deptIds)
//            .eq(SysUser::getBusinessType, UserBusinessType.USER_MEMBER);
//        if (type != null) {
//            wrapperForUser.eq(SysUser::getBusinessSubType, type);
//        }
//        List<SysUser> sysUsers = sysUserMapper.selectList(wrapperForUser);
//        if (CollUtil.isEmpty(sysUsers)) {
//            return R.ok(Lists.newArrayList());
//        }
//        List<MemberInfoVo> data = Lists.newArrayList();
//        List<Long> businessIds = sysUsers.stream().map(SysUser::getBusinessId).filter(Objects::nonNull).collect(Collectors.toList());
//        if (CollUtil.isNotEmpty(businessIds)) {
//            List<MemberInfoVo> memberInfoVos = memberInfoMapper.selectVoBatchIds(businessIds);
//            if (CollUtil.isNotEmpty(memberInfoVos)) {
//                data.addAll(memberInfoVos);
//            }
//        }
        LambdaQueryWrapper<MemberInfo> wrapper = Wrappers.<MemberInfo>lambdaQuery();
        if (type != null) {
            wrapper.in(MemberInfo::getType, type.getMemberTypeList());
        }
        List<MemberInfoVo> memberInfoVos = memberInfoMapper.selectVoList(wrapper);
        memberInfoService.renderSysUserInfo(memberInfoVos);
        return R.ok(memberInfoVos);
    }

}
